﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FSY.Components.DataProvider;
using FSY.API.CacheManager;
using FSY.API.Utilities;
using FSY.Globals;
namespace FSY.Components.Entities
{
    public class Fsy_RoleController
    {
        private static int InsertDeleteUpdate(Fsy_Role role, DataProviderAction action)
        {
            Fsy_Cache.RemoveCache(cacheRoleAll);
            Fsy_Cache.RemoveCache(cacheRoleByUser);
            Fsy_Cache.RemoveCache(cacheRoleByMenu);
            Fsy_Cache.RemoveCache(cacheRoleByModule);
            return Fsy_DataProvider.Instance.Role_AddDeleteUpdate(role, action);
        }
        public static int Insert(Fsy_Role role)
        {
            return InsertDeleteUpdate(role, DataProviderAction.Insert);
        }
        public static int Delete(int roleID)
        {
            Fsy_Role role = new Fsy_Role();
            role.RoleID = Fsy_Null.SetNullInteger(roleID);
            return InsertDeleteUpdate(role, DataProviderAction.Delete);
        }
        public static int Update(Fsy_Role role)
        {
            return InsertDeleteUpdate(role, DataProviderAction.Update);
        }

        private static string cacheRoleAll = "RoleALL";
        public static List<Fsy_Role> GetAllRole()
        {
            object objlock = new object();
            List<Fsy_Role> lsRole = new List<Fsy_Role>();
            lock (objlock)
            {
                lsRole = (List<Fsy_Role>)Fsy_Cache.GetCache(cacheRoleAll);
                if (lsRole == null)
                {
                    lsRole = Fsy_DataProvider.Instance.Role_GetListAll();
                    Fsy_Cache.SetCache(cacheRoleAll, lsRole, Fsy_Config.SlidingExpiration());
                    return lsRole;
                }
            }
            return lsRole;
        }
        public static Fsy_Role GetRoleByID(int roleID)
        {
            List<Fsy_Role> ls = GetAllRole();
            foreach (Fsy_Role role in ls)
            {
                if (role.RoleID == roleID)
                {
                    return role;
                }
            }
            return null;
        }
        private static string cacheRoleByUser = "roleByuser";
        private static object objlock = new object();
        public static List<Fsy_RoleQuery> GetRoleByUser(int userID)
        {            
            List<Fsy_RoleQuery> lsUser = new List<Fsy_RoleQuery>();
            lock (objlock)
            {
                lsUser = (List<Fsy_RoleQuery>)Fsy_Cache.GetCache(cacheRoleByUser);
                if (lsUser == null)
                {
                    lsUser = Fsy_DataProvider.Instance.Role_GetByUser(userID);
                    Fsy_Cache.SetCache(cacheRoleByUser, lsUser, Fsy_Config.SlidingExpiration());
                    return lsUser;
                }
            }
            return lsUser;
        }

        private static string cacheRoleByMenu = "roleByMenu";
        public static List<Fsy_RoleQuery> GetRoleByMenu(int menuID)
        {            
            List<Fsy_RoleQuery> lsUser = new List<Fsy_RoleQuery>();
            lock (objlock)
            {
                lsUser = (List<Fsy_RoleQuery>)Fsy_Cache.GetCache(cacheRoleByMenu);
                if (lsUser == null)
                {
                    lsUser = Fsy_DataProvider.Instance.Role_GetByMenu(menuID);
                    Fsy_Cache.SetCache(cacheRoleByMenu, lsUser, Fsy_Config.SlidingExpiration());
                    return lsUser;
                }
            }
            return lsUser;
        }

        private static string cacheRoleByModule = "roleByModule";
        public static List<Fsy_RoleQuery> GetRoleByModule(int moduleID)
        {            
            List<Fsy_RoleQuery> lsUser = new List<Fsy_RoleQuery>();
            lock (objlock)
            {
                lsUser = (List<Fsy_RoleQuery>)Fsy_Cache.GetCache(cacheRoleByModule);
                if (lsUser == null)
                {
                    lsUser = Fsy_DataProvider.Instance.Role_GetByModule(moduleID);
                    Fsy_Cache.SetCache(cacheRoleByModule, lsUser, Fsy_Config.SlidingExpiration());
                    return lsUser;
                }
            }
            return lsUser;
        }
    }
}
